/**
 * 
 */
package com.newworld.dmsapp2.util;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;

/**
 * 一个类 用来结束所有后台activity <br>
 * 使用方法：<br>
 * 在每个activity被创建时加上: SysApplication.getInstance().addActivity(this); <br>
 * 当你想关闭时，调用SysApplication的exit方法: SysApplication.getInstance().exit(); <br>
 * 异常退出时，调用SysApplication的exitError方法: SysApplication.getInstance().exitError();
 * 
 * @author Risa
 * 
 */
public class SysApplication extends Application {
	// 运用list来保存们每一个activity是关键
	private List<Activity> mList = new LinkedList<Activity>();
	// 为了实现每次使用该类时不创建新的对象而创建的静态对象
	private static SysApplication instance;

	private static PersistentCookieStore myCookieStore;

	// 构造方法
	private SysApplication() {
	}

	// 实例化一次
	public synchronized static SysApplication getInstance() {
		if (null == instance) {
			instance = new SysApplication();
		}
		return instance;
	}

	// add Activity
	public void addActivity(Activity activity) {
		mList.add(activity);
		Log.e("addActivity mList size = ", mList.size() + "");
	}

	// add Activity
	public void rmActivity(Activity activity) {
		mList.remove(activity);
		Log.e("rmActivity mList size = ", mList.size() + "");
	}

	@SuppressWarnings("unused")
	public void removeInstance() {
		for (Activity activity : mList) {
			// if (activity instanceof CheckItemsIndex || activity instanceof
			// ShowItems || activity instanceof CheckItems && activity!= null)
			// activity.finish();
		}
	}

	@SuppressWarnings("unused")
	public void removeCheckItems() {
		for (Activity activity : mList) {
			// if ( activity instanceof CheckItems ||activity instanceof
			// AddItems&& activity!= null)
			// activity.finish();
		}
	}

	// 关闭每一个list内的activity
	public void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

	// 杀进程
	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}

	// 关闭每一个list内的activity
	public void exitError() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(1);
		}
	}

	public boolean isActivityExist(Context context) {
		boolean result = false;
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		ComponentName cn = am.getRunningTasks(2).get(0).topActivity;
		if (cn != null) {
			for (Activity activity : mList) {
				String ee = activity.getClass().getName();
				String aa = cn.getClassName();
				Log.e("cn是" + aa, "list是" + ee);
				if (activity.getClass().getName().equals(cn.getClassName())) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public static PersistentCookieStore getMyCookieStore() {
		return myCookieStore;
	}

	public static void setMyCookieStore(PersistentCookieStore myCookieStore) {
		SysApplication.myCookieStore = myCookieStore;
	}

	public PersistentCookieStore getPersistentCookieStore() {

		return myCookieStore;

	}

}
